/**
 * Copyright(c) 2013-2015 by Puhuifinance Inc.
 * All Rights Reserved
 */
package com.puhui.chome.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.puhui.chome.controller.base.BaseController;
import com.puhui.chome.model.MonitorItem;
import com.puhui.chome.model.vo.QueryData;
import com.puhui.chome.service.MonitorItemService;
import com.puhui.chome.service.MonitorSystemService;
import com.puhui.chome.util.JdbcUtil;
import com.puhui.chome.util.StringUtil;

/**
 * @author dingxingang
 */
@Controller
@RequestMapping("/monitorItem")
public class MonitorItemController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(MonitorItemController.class);

    @Autowired
    private MonitorItemService monitorItemService;
    @Autowired
    private MonitorSystemService monitorSystemService;

    /**
     * 跳转列表页面
     * 
     * @author dingxingang
     * @return 跳转地址
     */
    @RequestMapping(value = "/toList")
    public String toList(ModelMap model) {
        // 查询监控系统
        model.put("systemList", monitorSystemService.findMonitorSystem(null));
        return "monitorItem/monitorItemList";
    }

    /**
     * 列表
     * 
     * @author dingxingang
     * @param monitorItemVo
     *            查询参数
     * @param page
     *            页码
     * @param size
     *            每页显示条数
     * @param sort
     *            排序
     * @param order
     *            正序or倒序
     * @return 列表
     */
    @ResponseBody
    @RequestMapping(value = "/monitorItemList", method = RequestMethod.POST)
    public Object monitorItemList(MonitorItem monitorItemVo, @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "rows", defaultValue = "50") int size,
            @RequestParam(value = "sort", defaultValue = "alarm") String sort,
            @RequestParam(value = "order", defaultValue = "desc") String order) {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        param.put("monitorItem", monitorItemVo);
        // 查询总数
        long total = monitorItemService.findMonitorItemCount(param);
        int from = (page - 1) * size; // 计算开始条数
        // 查询数据
        param.put("from", from);
        param.put("size", size);
        param.put("sort", StringUtil.getFieldName(sort));
        param.put("order", order);
        List<MonitorItem> list = new ArrayList<>();
        if (total > 0) {
            list = monitorItemService.findMonitorItem(param);
        }
        // 返回信息
        Map<String, Object> rev = new HashMap<>();
        rev.put("total", total);
        rev.put("rows", list);
        return rev;
    }

    /**
     * 跳转编辑页面
     * 
     * @author dingxingang
     * @param model
     *            消息
     * @param id
     *            主键
     * @return 跳转地址
     */
    @RequestMapping(value = "/toEdit")
    public String toEdit(ModelMap model, Long id) {
        if (id != null) {
            MonitorItem monitorItem = monitorItemService.getMonitorItemById(id);
            model.put("monitorItem", monitorItem);
        }
        // 查询监控系统
        model.put("systemList", monitorSystemService.findMonitorSystem(null));
        return "monitorItem/monitorItemEdit";
    }

    /**
     * 修改
     * 
     * @author dingxingang
     * @param monitorItem
     * @param model
     * @return
     */
    @RequestMapping("/upd")
    public String upd(MonitorItem monitorItem, ModelMap model) {
        if (monitorItem != null) {
            Long id = monitorItem.getId();
            // 新增用户
            if (id == null) {
                monitorItemService.addMonitorItem(monitorItem);
            } else { // 修改用户
                monitorItemService.updateMonitorItem(monitorItem);
            }
            model.put("monitorItem", monitorItem);
            model.put("message", "操作成功");
        }
        // 查询监控系统
        model.put("systemList", monitorSystemService.findMonitorSystem(null));
        return "monitorItem/monitorItemEdit";
    }

    /**
     * 启用、禁用监控项
     * 
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/enableMonitor", method = RequestMethod.POST)
    public Object enableMonitor(Long id, boolean enable, ModelMap model) {
        MonitorItem monitorItem = monitorItemService.getMonitorItemById(id);
        if (monitorItem != null) {
            monitorItem.setId(id);
            monitorItem.setEnable(enable);
            monitorItemService.updateMonitorItem(monitorItem);
            model.put("message", "操作成功");
        } else {
            model.put("message", "操作失败，未找到相应记录！");
        }
        return model;
    }

    /**
     * 重新检查
     * 
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/againInspect", method = RequestMethod.POST)
    public Object againInspect(Long id, ModelMap model) {
        MonitorItem monitorItem = monitorItemService.getMonitorItemById(id);
        if (monitorItem != null) {
            monitorItemService.monitorItem(monitorItem);
            model.put("success", true);
            model.put("message", "操作成功");
        } else {
            model.put("success", false);
            model.put("message", "操作失败，未找到相应记录！");
        }
        return model;
    }

    /**
     * 跳转报警数据查看页面
     * 
     * @author dingxingang
     * @return 跳转地址
     */
    @RequestMapping(value = "/toAlarmDataList")
    public String toAlarmDataList(Long itemId, ModelMap model) {
        model.put("itemId", itemId);
        return "monitorItem/monitorItemAlarmList";
    }

    /**
     * 报警数据
     * 
     * @author dingxingang
     * @param id
     *            监控项编号
     * @return 列表
     */
    @ResponseBody
    @RequestMapping(value = "/monitorItemAlarmList", method = RequestMethod.POST)
    public Object monitorItemAlarmList(Long itemId, ModelMap model) {
        if (itemId != null) {
            MonitorItem monitorItem = monitorItemService.getMonitorItemById(itemId);
            if (monitorItem != null && !StringUtils.isEmpty(monitorItem.getDataScript())) {
                logger.info("查询：" + monitorItem.getDataScript());
                QueryData data = JdbcUtil.queryData(monitorItem.getDataScript());
                if (data != null) {
                    model.put("fieldList", data.getFieldList());
                    model.put("dataList", data.getDataList());
                }
            }
        }
        return model;
    }

}
